3966. Passionate butterfly collector

 

As is well known, Andrey Sergeevich is an avid butterfly collector. His collection is enormous, containing specimens from all over the world. Let us assume that there are 2 * 109 species of butterflies in the world.

To stay organized, Andrey Sergeevich has assigned each species a unique number starting from one. Now he wants to determine whether a butterfly with number k is already in his collection or if he'll have to put considerable effort and resources into obtaining it.

 

Input. The first line contains an integer n (1 ≤ n ≤ 10⁵) – the number of butterfly species in Andrey Sergeevich’s collection.

The second line contains n distinct integers in increasing order – the numbers of the butterfly species currently in his collection.

The third line contains an integer m (1 ≤ m ≤ 10⁵) – the number of butterfly species Andrey Sergeevich wants to check for presence in his collection.

The fourth line contains m integers – the numbers of the butterfly species to be checked.

 

Output. Print m lines. For each query, print “YES” if the butterfly with the given number is in the collection, and “NO” otherwise.

 

Sample input

Sample output

7

10 47 50 63 89 90 99

4

84 33 10 82

NO

NO

YES

NO

 

 

SOLUTION

data structures – set

 

Algorithm analysis

Store the species numbers of butterflies in a set s. Then, to determine whether a butterfly of species x is in Andrey Sergeevich’s collection, simply check whether x belongs to the set s.

 

Algorithm implementation

Store all butterfly species numbers from the collection in the set s.

 

set<int> s;

 

Read the input data.

 

scanf("%d", &n);

for (i = 0; i < n; i++)

{

  scanf("%d", &x);

  s.insert(x);

}

 

Process m queries.

 

scanf("%d", &m);

for (i = 0; i < m; i++)

{

 

Check whether a butterfly of species x is in the collection. The answer is “YES” if the number x is present in the set s.

 

  scanf("%d", &x);

  if (s.find(x) != s.end()) puts("YES");

  else puts("NO");

}

 

Python implementation

Read the input data.

 

n = int(input())

lst = list(map(int,input().split()))

m = int(input())

q = list(map(int,input().split()))

 

Store all butterfly species numbers from the collection in the set s.

 

s = set(lst)

 

Process m queries.

 

for x in q:

 

Check whether a butterfly of species x is in the collection. The answer is “YES” if the number x is present in the set s.

 

  if x in s:

    print("YES")

  else:

    print("NO")